#!/bin/bash
#===============================================================================
# MPI测试程序编译脚本
# 使用方法：./compile.sh
#===============================================================================

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  编译MPI性能诊断工具${NC}"
echo -e "${BLUE}========================================${NC}"

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${SCRIPT_DIR}"

# 检查MPI编译器
if command -v mpicxx &> /dev/null; then
    MPI_CXX=mpicxx
elif command -v mpiCC &> /dev/null; then
    MPI_CXX=mpiCC
elif command -v mpic++ &> /dev/null; then
    MPI_CXX=mpic++
else
    echo -e "${RED}错误: 未找到MPI C++编译器 (mpicxx/mpiCC/mpic++)${NC}"
    exit 1
fi

echo -e "${GREEN}使用MPI编译器: ${MPI_CXX}${NC}"

# 编译选项
CXX_FLAGS="-g -O3 -march=native -fopenmp -std=c++11"
LIBS="-lm"

echo -e "${BLUE}编译选项: ${CXX_FLAGS}${NC}"

# 编译
echo -e "${BLUE}开始编译...${NC}"
${MPI_CXX} ${CXX_FLAGS} -o mpiTest mpiTest.cpp ${LIBS}

if [ $? -eq 0 ]; then
    echo -e "${GREEN}========================================${NC}"
    echo -e "${GREEN}  编译成功！${NC}"
    echo -e "${GREEN}========================================${NC}"
    echo -e "${GREEN}可执行文件: ${SCRIPT_DIR}/mpiTest${NC}"
    echo ""
    echo -e "运行方法："
    echo -e "  ${YELLOW}mpirun -np <进程数> ./mpiTest${NC}"
    echo ""
    echo -e "或使用自动化测试脚本："
    echo -e "  ${YELLOW}./run_mpi_scaling_test.sh${NC}"
else
    echo -e "${RED}========================================${NC}"
    echo -e "${RED}  编译失败！${NC}"
    echo -e "${RED}========================================${NC}"
    exit 1
fi

